package com.flash.javamate.utils;

import com.flash.javamate.handler.FindTextHandler;

import java.io.*;

/**
 * @author sunyiming
 * @date 2018/8/1
 */
public class SearchFileUtils {

    public static void findTextAndPrintLine(File root, FindTextHandler handler){
        try {
            func(root, handler);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void func(File file, FindTextHandler handler) throws IOException {
        File[] fs = file.listFiles();
        for (File f : fs) {
            if (f.isDirectory()) {
                func(f, handler);
            } else if (f.isFile()) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
                String line;
                while ((line = reader.readLine()) != null) {
                    line = line.trim();
                    if (handler.isMatch(line)) {
                        handler.formatPrint(line, f);
                    }
                }
            }
        }
    }

}
